{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> A Game with two biased coins </h2>\n",
    "\n",
    "Your friend Asja has one euro and one cent. \n",
    "\n",
    "Both coins are biased, and the probabilities of getting heads and tails are as follows:\n",
    "<ul>\n",
    "    <li> one euro: head with probability $ 0.6 $ and tail with probability $ 0.4 $. </li>\n",
    "    <li> one cent: head with probability $ 0.3 $ and tail with probability $ 0.7 $. </li>\n",
    "</ul>\n",
    "\n",
    "Asja flips her coins based on the following <b>protocol</b>: \n",
    "<ol> \n",
    "    <li> she starts with flipping one euro, </li>\n",
    "    <li> whenever she gets a head, she flips one euro, and </li>\n",
    "    <li> whenever she gets a tail, she flips one cent. </li>\n",
    "</ol>\n",
    "\n",
    "By using a single bit, we summarize all possible transitions of this game as follows:\n",
    "\n",
    "$\n",
    "GameCoins = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & 0.6 & 0.3\\\\  \\mathbf{Tail} & 0.4 & 0.7  \\end{array} = \\begin{array}{c|cc} & \\mathbf{0} & \\mathbf{1} \\\\ \\hline \\mathbf{0} & 0.6 & 0.3 \\\\  \\mathbf{1} & 0.4 & 0.7  \\end{array}\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Task 1: Convience yourself </h3>\n",
    "\n",
    "Please convience yourself about the correctness of transitions given in the table.\n",
    "\n",
    "<i> Remark that there is no difference between getting head from one euro or getting head from one cent.\n",
    "    \n",
    "Therefore, one bit is enough to represent all transitions.\n",
    "</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Tracing Asja's three coin tosses </h3>\n",
    "\n",
    "Suppose that Asja <b>secretly</b> tosses her coins based on the defined protocol.\n",
    "\n",
    "By using python, we can calculate the probabilities of Asja getting head and tail after three coin tosses.\n",
    "\n",
    "<i><b>Remark:</b> In the previous two tasks, the ideal ratio was easy to calculate. We did our experiments and compared our results with the ideal ratio. \n",
    "\n",
    "Here we will calculate the exact probabilities (the ideal ratio) by using python. (We will not do experiment.)\n",
    "</i>\n",
    "\n",
    "We present our solution step by step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# OUR SOLUTION\n",
    "#\n",
    "\n",
    "# initial case\n",
    "# We assume that the probability of getting head is 1 at the beginning,\n",
    "#    because Asja will start with one euro.\n",
    "prob_head = 1\n",
    "prob_tail = 0\n",
    "\n",
    "\n",
    "#\n",
    "# first coin-flip\n",
    "#\n",
    "\n",
    "# if the last result was head\n",
    "new_prob_head_from_head = prob_head * 0.6\n",
    "new_prob_tail_from_head = prob_head * 0.4\n",
    "\n",
    "# if the last result was tail\n",
    "# we know that prob_tail is 0 at the beginning \n",
    "#     but we still keep these two lines to have the same code for each iteration\n",
    "new_prob_head_from_tail = prob_tail * 0.3\n",
    "new_prob_tail_from_tail = prob_tail * 0.7\n",
    "\n",
    "# update the probabilities at the end of coin toss\n",
    "prob_head = new_prob_head_from_head + new_prob_head_from_tail\n",
    "prob_tail = new_prob_tail_from_head + new_prob_tail_from_tail\n",
    "\n",
    "#\n",
    "# second coin-flip\n",
    "#\n",
    "# we do the same calculations\n",
    "\n",
    "new_prob_head_from_head = prob_head * 0.6\n",
    "new_prob_tail_from_head = prob_head * 0.4\n",
    "\n",
    "new_prob_head_from_tail = prob_tail * 0.3\n",
    "new_prob_tail_from_tail = prob_tail * 0.7\n",
    "\n",
    "prob_head = new_prob_head_from_head + new_prob_head_from_tail\n",
    "prob_tail = new_prob_tail_from_head + new_prob_tail_from_tail\n",
    "\n",
    "#\n",
    "# third coin-flip\n",
    "#\n",
    "# we do the same calculations\n",
    "\n",
    "new_prob_head_from_head = prob_head * 0.6\n",
    "new_prob_tail_from_head = prob_head * 0.4\n",
    "\n",
    "new_prob_head_from_tail = prob_tail * 0.3\n",
    "new_prob_tail_from_tail = prob_tail * 0.7\n",
    "\n",
    "prob_head = new_prob_head_from_head + new_prob_head_from_tail\n",
    "prob_tail = new_prob_tail_from_head + new_prob_tail_from_tail\n",
    "\n",
    "# print prob_head and prob_tail\n",
    "print(\"the probability of getting head\",prob_head)\n",
    "print(\"the probability of getting tail\",prob_tail)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2: Tracing ten biased coin tosses </h3>\n",
    "\n",
    "By using python, calculate the probabilities of Asja getting head and tail after 10 coin tosses.\n",
    "\n",
    "$\n",
    "GameCoins = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & 0.6 & 0.3\\\\  \\mathbf{Tail} & 0.4 & 0.7  \\end{array} = \\begin{array}{c|cc} & \\mathbf{0} & \\mathbf{1} \\\\ \\hline \\mathbf{0} & 0.6 & 0.3 \\\\  \\mathbf{1} & 0.4 & 0.7  \\end{array}\n",
    "$\n",
    "\n",
    "Please use a loop in your solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B28_Coin_Flip_Game_Solution.ipynb#task2\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
